/***************************init***************/
function queryCommunication_communication(page){
	sys_loading();
	var parm = $('#queryCommunicationForm').serialize() + (page != null ? ('&page=' + page) : '');
	var url = create_url('communication-query');
	sys_record_parm('communication',parm);
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_loading_close();
			$('#communicationResultList').html(result);
		}
	});
}


function edit_communication(code) {
	var parm = {'code':code};
	var url = create_url('communication-edit');
	sys_submiting();
	$.ajax({type:'POST', url:url, data:parm});
}

function deleteCommunication_communication(code, name) {
	var url = create_url('communication-delete');
		$.Zebra_Dialog('要删除  ' + name + '  通讯录记录吗?',{
			'type': 'question',
			'title': '警告' ,
			'buttons':[
		           {caption:'确认',callback:function() {
		        	   communication_aidExecuteCommunication(code, url);
		    		}},
		           {caption:'取消',callback:function(){}
		    	}]
	});
}

function aidExecuteCommunication_communication(code, url) {
	var parm = {'code':code};
	sys_submiting();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_submiting_close();
			if ('success' == result) {
				communication_queryCommunication();
			} else if ('deleted' == result){ 
				new $.Zebra_Dialog('你不能删除指派的任务', {
				    'buttons':  false,
				    'modal': false,
				    'auto_close': 2000
				});
			} else {
				new $.Zebra_Dialog('操作失败，请刷新页面再次尝试，如果问题依然存在，请联系管理员!', {
				    'buttons':  false,
				    'modal': false,
				    'auto_close': 2000
				});
			}
		}
	});
}

/*******************************edit***********************/


/**
 * 验证Email
 * @param {} email
 * @return {Boolean}
 */
function validateEmail(email) {
	var reEmail = /^([A-Za-z0-9])(\w)+@(\w)+(\.)(com|com\.cn|net|cn|net\.cn|org|biz|info|gov|gov\.cn|edu|edu\.cn)/;

	return email.match(reEmail) || email == " ";
	if(email== null){
		return false;
		}
}
/**
 * 验证电话号码
 * @param phone
 * @returns {Boolean}
 */
function verifyPhone(phone) {
	var rePhone = /^0?1[3|4|5|8][0-9]\d{8}$/;//手机

	return rePhone.test(phone) || phone == " ";
	if(phone== null){
		return false;
		}
}


function saveCommunication_communication() {
	var email = $('#email').val();
	if("" != email){
		if (!validateEmail(email)) {
			sys_msg('对不起！该邮箱名【' + email + '】格式不正确!',{
				'type':	'error',
				'title': '错误'
			});
			return false;
		}
	}
	var parm = $('#editCommunicationForm').serialize();
	var url = create_url('communication-save');
	sys_submiting();
	$.ajax({type:'POST', url:url, data:parm,
		success: function(result) {
			sys_submiting_close();
			if (result == 'success') {
				sys_msg('操作成功');
				sys_load_handle('communication');
			}else if(result == 'not_find'){
				sys_msg('姓名或电话不能为空', '警告');
			} else {
				sys_msg('操作失败', '警告');
			}
		}
	});
}

function excelImport () {
	var url = create_url('communication-excelImport');
	var btn = $('#excelBtn');
	new AjaxUpload(btn, {
		action:url,
		name:'files',
		multiple: true,
		responseType: 'json',
		onSubmit : function(file, ext) {
			if (ext && /^(xls|xlsx)$/.test(ext)){
				this.setData({
					'info':'文件类型正确'
				});
			} else {
				sys_msg('文件类型错误,<br/>只能上传类型为：xls、xlsx的文件!');
				return false;               
			}
			$('#excelBtn_span').html(sys_loading_img);
			//this.disable();
		},
		onComplete: function(files, response) {
			if ('success' == response.result) {
				sys_msg('导入成功');
				queryCommunication_communication();
			} else if ('error' == response.result && '1' == response.msg)
				sys_msg('您傻上传的文件不能被解析');
			else 
				sys_msg('导入失败');
			
		}  
	});
}